package cn.powerchaintech.common.task;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static java.time.temporal.ChronoUnit.SECONDS;

public abstract class IntervalTask extends TaskExecutor {
    private long delaySecond;

    public IntervalTask(String taskName, long second) {
        super(taskName, second * 1000);
        this.delaySecond = 0;
    }

    public IntervalTask(String taskName, long second, long delaySecond) {
        super(taskName, second * 1000);
        this.delaySecond = delaySecond * 1000;
    }

    public long getDelaySecond() {
        return delaySecond;
    }

    @Override
    public TaskType taskType() {
        return TaskType.INTERVAL;
    }

    @Override
    public void notifySuccess() {

    }

    @Override
    public List<LocalDateTime> computeFireTimesBetween(LocalDateTime from, LocalDateTime to) {
        List<LocalDateTime> lst = new ArrayList<>();
        LocalDateTime ldt = getLastSuccessExecuteDateTime();
        while(true) {
            ldt = ldt.plus(getSecond(), SECONDS);
            if(ldt.isAfter(from) && ldt.isBefore(to)) {
                lst.add(ldt);
                continue;
            }
            break;
        }
        return lst;
    }
}
